package storage

import (
	"time"
	"web_framework/model"

	"github.com/jmoiron/sqlx"
)

func CreateComment(db sqlx.Ext, qi *model.Comment) error {
	qi.CreateTime = time.Now()
	qi.UpdateTime = time.Now()
	if qi.Top {
		qi.TopTime = &qi.UpdateTime
	}

	_, err := sqlx.NamedExec(db,
		`insert into comment values (
			:comment_id,         
			:main_cmoment_id,    
			:reply_comment_id,   
			:from_uid,           
			:to_uid,             
			:itemid,             
			:channel_id,         
			:season,             
			:episode,            
			:content,            
			:good,               
			:bad,                
			:vs,                 
			:top,                
			:top_time,           
			:client,             
			:create_ip,          
			:create_time,        
			:update_time,        
			:status             
		)`, *qi,
	)
	if err != nil {
		return err
	}

	return nil
}

func GetComment(db sqlx.Queryer, id int) (model.Comment, error) {
	var qi model.Comment
	err := sqlx.Get(db, &qi, "select * from comment where comment_id = ? and status = true", id)
	if err != nil {
		return qi, err
	}
	return qi, nil
}

func UpdateComment(db sqlx.Execer, qi *model.Comment) error {
	qi.UpdateTime = time.Now()
	if qi.Top {
		qi.TopTime = &qi.UpdateTime
	}

	_, err := db.Exec(`
		update comment set 		         
			content = ?,            
			good = ?,               
			bad = ?,                
			vs = ?,                 
			top = ?,                
			top_time = ?,           
			client = ?,                       
			update_time = ?   
		where
			comment_id = ?`,
		qi.Content,
		qi.Good,
		qi.Bad,
		qi.Vs,
		qi.Top,
		qi.TopTime,
		qi.Client,
		qi.UpdateTime,
		qi.CommentID,
	)
	if err != nil {
		return err
	}

	return nil
}

func DeleteCommentSoft(db sqlx.Execer, id int) error {
	_, err := db.Exec(`
		update comment set 		         
			status = ?          
		where
			comment_id = ?`,
		false,
		id,
	)
	if err != nil {
		return err
	}

	return nil
}

func GetCommentsFromItemAndChannel(db sqlx.Queryer, itemID int, channelID int) ([]model.Comment, error) {
	var items []model.Comment

	err := sqlx.Select(db, &items, `
        select
            *
        from
            comment
		where
			status = true
			and itemid = ? 
			and channel_id = ?`,
		itemID, channelID,
	)
	if err != nil {
		return nil, err
	}

	return items, nil
}

func ResetCommentTable(db sqlx.Execer) error {

	_, err := db.Exec(`drop table if exists comment;`)
	if err != nil {
		return err
	}

	_, err = db.Exec(`
	create table comment
	(
	   comment_id           int not null auto_increment comment '自增长ID',
	   main_cmoment_id      int comment '主回复ID',
	   reply_comment_id     int default 0 comment '回复ID， 0为主评论',
	   from_uid             int not null comment '发布用户id',
	   to_uid               int comment '接收用户id',
	   itemid               int not null comment '项目id',
	   channel_id           int not null default 0 comment '频道id',
	   season               int default 0 comment '季',
	   episode              int default 0 comment '集',
	   content              text default NULL comment '内容',
	   good                 int default 0 comment '好评数',
	   bad                  int default 0 comment '差评数',
	   vs                   tinyint default 0 comment 'vs支持 1-红方 2-蓝方',
	   top                  tinyint default 0 comment '是否被置顶 0否 1是',
	   top_time             datetime default NULL comment '置顶时间',
	   client               tinyint default 0 comment '1-IOS 2-安卓 3-WP 0-PC',
	   create_ip            varchar(40) not null default '' comment '作者IP',
	   create_time          datetime not null comment '创建时间',
	   update_time          datetime default NULL comment '修改时间',
	   status               tinyint not null default 1 comment '状态 1 正常 0 无效 默认 1',
	   primary key (comment_id),
	   key ak_c_u (from_uid),
	   key ak_c_i_c_i (itemid, channel_id)
	)
	ENGINE = InnoDB
	DEFAULT CHARACTER SET = utf8;
	`)
	if err != nil {
		return err
	}

	_, err = db.Exec(`alter table comment comment '评论表';`)
	if err != nil {
		return err
	}

	return nil
}
